package ferp.android.dialogs;

import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.text.Html;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.ViewFlipper;
import ferp.android.R;
import ferp.android.managers.PollManager;
import ferp.poll.Question;

/**
 * User: olegs
 * Date: 19/05/2014 13:29
 */
public class PollDialog extends Dialog implements ChoiceDialog.ListData.OnItemSafeClickListener
{
  public static PollDialog create(Context context, final PollManager.Listener listener)
  {
    return PollManager.get() == null ? null : new PollDialog(context, listener);
  }

  @Override
  public void onItemClicked(int i)
  {
    Question q = PollManager.onAnswer(getContext(), sp, listener, i);

    if (q == null)
      dismiss();
    else
      show(q);
  }

  public void prepare() {show(PollManager.question());}

  private PollDialog(Context context, final PollManager.Listener listener)
  {
    super(context, R.layout.dialog_poll);

    this.listener = listener;

    flipper = (ViewFlipper)findViewById(R.id.dlg_poll_flipper);

    step0 = createStep(R.id.dlg_poll_step_0);
    step1 = createStep(R.id.dlg_poll_step_1);

    flipper.setInAnimation(context, R.anim.flip_in_from_right);
    flipper.setOutAnimation(context, R.anim.flip_out_to_left);
    flipper.setMeasureAllChildren(false);

    step0.setOnItemSafeClickListener(this);
    step1.setOnItemSafeClickListener(this);

    sp = PreferenceManager.getDefaultSharedPreferences(context);

    title.setText(context.getResources().getString(R.string.poll_caption) + " - " + PollManager.get().title());

    setOnCancelListener(new OnCancelListener()
    {
      @Override
      public void onCancel(DialogInterface dialogInterface)
      {
        PollManager.onCancel();
      }
    });

    setOnShowListener(new OnShowListener()
    {
      @Override
      public void onShow(DialogInterface dialogInterface)
      {
        step0.onShow();
        step1.onShow();
      }
    });
  }

  private ChoiceDialog.ListData createStep(int layoutID)
  {
    return new ChoiceDialog.ListData(getContext(),
                                     (TextView)(findViewById(layoutID).findViewById(R.id.lvb_text)),
                                     (ListView)(findViewById(layoutID).findViewById(R.id.lvb_list)),
                                     ListView.CHOICE_MODE_SINGLE, android.R.layout.simple_list_item_single_choice);
  }

  private void show(Question q)
  {
    View v = flipper.getChildAt(0);

    // swap steps (current step can be reused for the next one)
    flipper.removeViewAt(0);
    flipper.addView(v);

    // get next step
    ChoiceDialog.ListData next = v.getId() == R.id.dlg_poll_step_0 ? step0 : step1;

    // prepare next step
    next.set(Html.fromHtml(q.text() + "<br>"), q.answers());

    // show next step
    flipper.showNext();
  }

  private final SharedPreferences sp;
  private final PollManager.Listener listener;

  private final ViewFlipper flipper;
  private final ChoiceDialog.ListData step0;
  private final ChoiceDialog.ListData step1;
}
